(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:fuwuList
     * @description
     * # fuwuList 
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.authorization.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('authorization', function() {
            return {
                restrict: 'E',
                templateUrl: "views/directives.authorization.html",
                // template: "<a href='javascript:void(0);'   ng-click='showPop();'>{{title | translate}}</a> ",
                scope: {
                    title: "@",
                    auth: "@",
                    callback: "&"
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', 'ERP_CONFIG', '$location',

                    function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, ERP_CONFIG, $location) {
                        $scope.Status = "ziji";
                        $scope.store_id = $rootScope.store_id;
                        //授权网址
                        $scope.authUrl = function() {
                            return dataService.authorizePreAuthCodeGet().then(function(result) {
                                $scope.isvappid = ERP_CONFIG.IsvAppID;
                                if (!result.pre_auth_code) {
                                    helper.myAlert("错误：" + result.Msg + "。请联系客服解决！");
                                } else {
                                    $scope.preCode = result.pre_auth_code;
                                    var backUrl = encodeURIComponent($location.$$absUrl.split("#")[0].replace("/admin.html", "/clear.html") + "#?store_id=" + $scope.store_id);
                                    $scope.appAuthUrl = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=" + ERP_CONFIG.IsvAppID + "&pre_auth_code=" + $scope.preCode + "&redirect_uri=" + backUrl;
                                    return $scope.appAuthUrl;
                                }
                            });
                        }
                        //保存授权
                        $scope.saveAuth = function(data) {
                            $scope.ceishi = 0;
                            console.error("saveAuth callback:", data, ERP_CONFIG.IsvAppID);
                            if (data.auth_code) {
                                dataService.authorizeAuthCodeGet(ERP_CONFIG.IsvAppID, data.auth_code, data.store_id).then(function(result) {
                                    console.log("授权保存结果：", result);
                                    if (result.Msg) {
                                        helper.myAlert("错误：" + result.Msg);
                                    } else {
                                        // helper.myAlert("授权成功！");
                                        $scope.getOneauthrizeApp(result.cust_account_id);
                                    }
                                });
                            } else {
                                helper.myAlert("授权出错，请重试！");
                            }
                        }

                        $scope.getOneauthrizeApp = function(cust_account_id) {
                            $scope.storeAppList =[];
                            var datas = {
                                query:{cust_account_id: cust_account_id} 
                            };
                            dataService.custaccountGet(datas).then(function(result) {
                                if (result) {
                                    $scope.storeAppList = result.cust_account ;
                                    if ($scope.callback) {
                                        $scope.callback({ data: result.cust_account[0] });
                                    }
                                }
                            });
                        }

                        $scope.getisv = function() {
                            dataService.phpisvwxappGet().then(function(result) {
                                if (result) {
                                    $scope.storeAppList = result;
                                    if ($scope.callback) {
                                        $scope.callback({ data: result[0] });
                                    }
                                    console.error($scope.storeAppList);
                                }
                            });
                        }

                        $scope.showPop = function() {
                            $.alert({
                                backdrop: 'static',
                                keyboard: true,
                                title: '{{"授权小程序" | translate}}',
                                body: $templateCache.get("views/directives.authorization.html"),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: 'large',
                                hasfoot: false,
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {

                                },
                            });
                        }

                       
                    }
                ]
            }
        })
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.layoutstyle.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('layoutStyle', function() {
            return {
                restrict: 'E',
                templateUrl: "views/directives.layoutstyle.html",
                // template: "<a href='javascript:void(0);'   ng-click='showPop();'>{{'选择装修风格' | translate}}</a> ",
                scope: {
                    callback: "&"
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', 'ERP_CONFIG', '$location',

                    function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, ERP_CONFIG, $location) {
                        $scope.store_id = $rootScope.store_id;
                        var data = '[{"name":"colorstylechoice","type":"colorStyleChoice","title":"颜色风格选择","value":"1"},{"name":"time_backColor","type":"color","title":"倒计时背景颜色","value":"#ffe04a"},{"name":"zujian_backColor","type":"color","title":"组件背景颜色","value":"#1dbb90"},{"name":"zujian_backFontColor","type":"color","title":"组件背景里的字体颜色","value":"#ffffff"},{"name":"describe_fontColor","type":"color","title":"描述级说明文字颜色","value":"#a0a0a0"},{"name":"detail_cartBtnColor","type":"color","title":"商品详情购物车按钮","value":"#effbf9"},{"name":"page_titleColor","type":"color","title":"页面标题标准字体颜色","value":"#4e4e4e"},{"name":"zujian_fenge","type":"hidden","value":"https://img.alicdn.com/imgextra/i2/20841398/O1CN011MCK9cM59vdMqe5_!!20841398.png"},{"name":"home_img","type":"img","value":"https://img.alicdn.com/imgextra/i3/20841398/O1CN011MCK9q5ENHMVXTE_!!20841398.jpg"},{"name":"detail_img","type":"img","value":"https://img.alicdn.com/imgextra/i1/20841398/O1CN011MCK9xgSdwpG3Zf_!!20841398.jpg"}]';
                        $scope.showPop = function() {
                            $scope.Publics = { data: JSON.parse(data) };
                        }
                        $scope.showPop();
                        $scope.colorBack = function(data) {
                            if ($scope.callback) {
                                $scope.callback({ data: $scope.Publics });
                            }
                        }
                    }
                ]
            }
        })
})();